package com.chinaunicom.create.singleton.hungry;

import java.util.Random;

/**
 * @author liry
 * @version 1.0
 * @date Created on 2022/7/14 14:22
 * Description: 懒汉式(一般写法): 存在线程安全问题
 */
public class HSingleton01 {
    
    private static HSingleton01 instance;
    
    private  HSingleton01(){}
    
    public static HSingleton01 getInstance(){
        if ( instance == null){
            instance = new HSingleton01();
        }
        return instance;
    }


    public static void main(String[] args) {
        Random random = new Random();
        for(int i = 0; i < 10; i++) {
           new Thread(new Runnable() {
               @Override
               public void run() {
                   try {
//                       Thread.sleep(random.nextInt(100));
                       System.out.println(HSingleton01.getInstance().toString());
                   } catch (Exception e) {
                       e.printStackTrace();
                   }
                   
               }
           }, "Thread name: "+ i).start();
        }
        
    }
}
